package com.bwie.home.backstage.controller;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bwie.home.backstage.aop.Token;
import com.bwie.home.backstage.entity.CommunityEntity;
import com.bwie.home.backstage.entity.RankEntity;
import com.bwie.home.common.config.CreateToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.home.backstage.entity.HousingEntity;
import com.bwie.home.backstage.service.HousingService;
import com.bwie.home.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author sy
 * @email sunlightcs@gmail.com
 * @date 2021-09-10 09:46:41
 */
@RestController
@RequestMapping("backstage/housing")
public class HousingController {
    @Autowired
    private HousingService housingService;

    @Autowired
    private CreateToken createToken;

    @PostMapping("/getToken")
    public R getToken(@RequestBody HousingEntity housingEntity){
        String token = createToken.create(housingEntity.getCname(), "housing");
        return R.ok().put("token",token);
    }

    /**
     * 获取二手房/出租房的房源列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(value = "page",defaultValue = "1")int page,
                  @RequestParam(value = "limit",defaultValue = "2")int limit,
                  @RequestParam(value = "title",defaultValue = "")String title,
                  @RequestParam(value = "type",defaultValue = "0")int type,@RequestBody CommunityEntity community){
        IPage<HousingEntity> pa=housingService.getList(page,limit,title,type,community);

        return R.ok().put("page", pa);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{housingId}")
    public R info(@PathVariable("housingId") Integer housingId){
		HousingEntity housing = housingService.getById(housingId);

        return R.ok().put("housing", housing);
    }

    /**
     * 保存
     */
    @Token
    @RequestMapping("/save")
    public R save(@RequestBody HousingEntity housing, HttpServletRequest request){
		housingService.save(housing);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HousingEntity housing){
		housingService.updateById(housing);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] housingIds){
		housingService.removeByIds(Arrays.asList(housingIds));

        return R.ok();
    }

}
